package com.lyshop.jinding.activitys;

import static com.lyshop.jinding.utils.RouteUtil.gotoActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.lyshop.jinding.R;
import com.lyshop.jinding.base.BaseActivity;
import com.lyshop.jinding.bean.OrderInfoBean;
import com.lyshop.jinding.constants.Constant;
import com.lyshop.jinding.eventbus.EventCenter;
import com.lyshop.jinding.eventbus.EventConstants;
import com.lyshop.jinding.utils.RouteUtil;
import com.zc.mychart.model.GoodsAllBean;
import com.zc.mychart.utils.ArithUtil;
import com.zc.mychart.utils.FormatUtil;

import java.util.Map;

public class OrderDetailActivity extends BaseActivity implements View.OnClickListener {

    private OrderInfoBean mOrderBean;
    private RelativeLayout rl_sell_layout, rl_lastest_price, rl_liqu_price;
    private TextView tv_label_01, tv_label_02, tv_label_03, tv_label_04, tv_label_05, tv_label_06, tv_label_07, tv_label_08,
            tv_label_09, tv_label_10, tv_label_11, tv_label_12, tv_label_13, tv_label_14, tv_label_15, tv_label_16;


    private String url = "";
    private String model = "";
    private boolean mIsHistory;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mOrderBean = (OrderInfoBean) getIntent().getSerializableExtra(RouteUtil.BUNDLE_ORDER_MODEL);
        mIsHistory = getIntent().getBooleanExtra(RouteUtil.BUNDLE_IS_HISTORY, false);

        setData();
    }

    private void setData() {
        if (null != mOrderBean) {
            if (mIsHistory){
                rl_lastest_price.setVisibility(View.GONE);
                rl_liqu_price.setVisibility(View.VISIBLE);
                rl_sell_layout.setVisibility(View.VISIBLE);

                tv_label_16.setText(FormatUtil.formatDouble2(mOrderBean.liqui_price));
                String liquTime = FormatUtil.getDataYMDFromTimestamp(Long.parseLong(mOrderBean.liqui_time));
                tv_label_13.setText(liquTime);

                int flag = mOrderBean.trade_type == 2 ? 1 : -1;
                double diff = ArithUtil.sub(mOrderBean.liqui_price, mOrderBean.build_price) * flag;
                double singleProfit = ArithUtil.mul(diff, mOrderBean.k_amount);
                double totalProfit = ArithUtil.mul(singleProfit, mOrderBean.amount);
                totalProfit = ArithUtil.mul(totalProfit, mOrderBean.multiple);
                mOrderBean.profitAndLoss = totalProfit;
                setProfitLoss();
            }else {
                rl_lastest_price.setVisibility(View.VISIBLE);
                rl_liqu_price.setVisibility(View.GONE);
                rl_sell_layout.setVisibility(View.GONE);

                tv_label_05.setText(FormatUtil.formatDouble2(mOrderBean.new_price));
            }

            tv_label_01.setText(mOrderBean.product_name);
            tv_label_02.setText(mOrderBean.order_id);
            tv_label_03.setText(mOrderBean.trade_type == 2 ? "现价" : "结算价");

            tv_label_06.setText(FormatUtil.formatDouble2(mOrderBean.build_price));

            String orderMoney = mOrderBean.use_ticket == 1 ? "0" : FormatUtil.formatDouble2(ArithUtil.mul(mOrderBean.amount, mOrderBean.unit_price));
            tv_label_07.setText(String.valueOf(mOrderBean.amount));
            tv_label_08.setText(orderMoney + "元");


            tv_label_09.setText(mOrderBean.target_profit + "点");
            tv_label_10.setText(mOrderBean.stop_loss + "点");

            String moneyType = mOrderBean.use_ticket == 1 ? "代金券" : "账户余额";
            tv_label_11.setText(moneyType);

            String orderTime = FormatUtil.getDataYMDFromTimestamp(Long.parseLong(mOrderBean.build_time));
            tv_label_12.setText(orderTime);

            tv_label_15.setText(String.format("%s元", FormatUtil.formatDouble2(mOrderBean.trade_fee)));

            setProfitLoss();

            model = mOrderBean.trade_type == 2 ? "现价订购" : "结算价订购";
        }
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_order_info;
    }

    @Override
    protected void findViews() {
        rl_sell_layout = findViewById(R.id.rl_sell_layout);
        rl_lastest_price = findViewById(R.id.rl_lastest_price);
        rl_liqu_price = findViewById(R.id.rl_liqu_price);

        tv_label_01 = findViewById(R.id.tv_label_01);
        tv_label_02 = findViewById(R.id.tv_label_02);
        tv_label_03 = findViewById(R.id.tv_label_03);
        tv_label_04 = findViewById(R.id.tv_label_04);
        tv_label_05 = findViewById(R.id.tv_label_05);
        tv_label_06 = findViewById(R.id.tv_label_06);
        tv_label_07 = findViewById(R.id.tv_label_07);
        tv_label_08 = findViewById(R.id.tv_label_08);
        tv_label_09 = findViewById(R.id.tv_label_09);
        tv_label_10 = findViewById(R.id.tv_label_10);
        tv_label_11 = findViewById(R.id.tv_label_11);
        tv_label_12 = findViewById(R.id.tv_label_12);
        tv_label_13 = findViewById(R.id.tv_label_13);
        tv_label_14 = findViewById(R.id.tv_label_14);
        tv_label_15 = findViewById(R.id.tv_label_15);
        tv_label_16 = findViewById(R.id.tv_label_16);
    }

    @Override
    protected void setListeners() {
        tv_label_14.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_label_14:
                String orderMoney = mOrderBean.use_ticket == 1 ? "0" : FormatUtil.formatDouble2(ArithUtil.mul(mOrderBean.amount, mOrderBean.unit_price));
                Bundle bundle = new Bundle();
                bundle.putString("product", mOrderBean.product_name);//商品名
                bundle.putString("code", mOrderBean.product_code);//商品代码
                bundle.putString("count", mOrderBean.amount + "");//份数
                bundle.putString("model", mOrderBean.trade_type == 2 ? "现价订购" : "结算价订购");//模式
                bundle.putString("price", orderMoney);//定金
                bundle.putString("order", mOrderBean.order_id);//订单号
                bundle.putString("fee", String.format("%s元", FormatUtil.formatDouble2(mOrderBean.trade_fee)));//服务费
                gotoActivity(mContext, BuyAgreementActivity.class, bundle);
                break;
        }
    }


    private void routeAgreement() {
        url = Constant.H5_PRODUCT_AGREEMENT;
        RouteUtil.toWebViewActivity((BaseActivity) mContext, url);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onEventComming(EventCenter eventCenter) {
        super.onEventComming(eventCenter);
        int eventCode = eventCenter.getEventCode();
        switch (eventCode) {
            case EventConstants.EVENTCODE_UPDATE_PRO_PRICE:
                //开始更新列表
//                LogUtil.d("dzt====开始更新");
                if (!mIsHistory) {
                    changeOrderPrice();
                }
                break;
        }
    }

    private void changeOrderPrice() {
        Map<String, GoodsAllBean> allGoodMap = myApplication.getAllGoodMap();
        GoodsAllBean goodsAllBean = allGoodMap.get(mOrderBean.pro_code);
        double newPrice = goodsAllBean.latest_price;
        mOrderBean.new_price = newPrice;
        mOrderBean.pro_name = goodsAllBean.pro_name;
        mOrderBean.multiple = goodsAllBean.multiple;
        setProfitAndLoss(newPrice, mOrderBean);
    }

    private void setProfitAndLoss(double latestPrice, OrderInfoBean order) {
        int flag = order.trade_type == 2 ? 1 : -1;
        int target = order.target_profit;
        int stop = order.stop_loss * -1;
        if (stop == 0) {
            stop = order.max_stop_loss * -1;
        }
        double diff = ArithUtil.sub(latestPrice, order.build_price) * flag;
        if (target != 0 && diff >= target) {
            diff = target;
        }
        if (diff <= stop) {
            diff = stop;
        }
        if (diff < 0 && order.use_ticket == 1) {
            diff = 0;
        }
        double singleProfit = ArithUtil.mul(diff, order.k_amount);
        double totalProfit = ArithUtil.mul(singleProfit, order.amount);
        totalProfit = ArithUtil.mul(totalProfit, order.multiple);
        order.profitAndLoss = totalProfit;

        setProfitLoss();
    }

    private void setProfitLoss() {
        int redColor = ContextCompat.getColor(mContext, R.color.buy_red);
        int greenColor = ContextCompat.getColor(mContext, R.color.buy_green);

        if (mOrderBean.profitAndLoss >= 0) {
            tv_label_04.setTextColor(redColor);
        } else {
            tv_label_04.setTextColor(greenColor);
        }
        tv_label_05.setText(FormatUtil.formatDouble2(mOrderBean.new_price));
        String profit = mOrderBean.profitAndLoss >= 0 ? FormatUtil.formatMoney(mOrderBean.profitAndLoss) + "元" : mOrderBean.use_ticket == 1 ? "0元" : FormatUtil.formatMoney(mOrderBean.profitAndLoss) + "元";
        tv_label_04.setText(profit);
    }


}
